import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_novel/detail/bloc/home_commits_list_bloc.dart';
import 'package:flutter_novel/detail/bloc/home_detail_bloc.dart';
import 'package:flutter_novel/detail/page/home_commits_list.dart';
import 'package:flutter_novel/detail/page/home_detail_page.dart';
import 'package:flutter_novel/routers/routers.dart';
import 'package:flutter_novel/series_list/bloc/series_list_bloc.dart';
import 'package:flutter_novel/series_list/page/series_list_page.dart';
import 'package:flutter_novel/until/simpleroute_observer.dart';
import 'package:flutter_novel/widget/loading_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    MultiBlocProvider(
      providers: [
        BlocProvider<HomeDetailBloc>(create: (BuildContext context) => HomeDetailBloc()),
        BlocProvider<SeriesListBloc>(create: (BuildContext context) => SeriesListBloc()),
        BlocProvider<HomeCommitsListBloc>(create: (BuildContext context) => HomeCommitsListBloc()),
      ],
      child: const MyHomePage(),
    ),
  );
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    NovelRoutes.initRoutes();
    LoadingUtil.initialize();
  }

  MaterialApp _materialApp(BuildContext context) {
    return MaterialApp(
      navigatorObservers: [simpleObserver],
      // initialRoute: '/detail',
      routes: {
        '/detail': (context) => HomeDetailPage(imageUrl: ''),
        '/series': (context) => SeriesListPage(),
        '/commits': (context) => HomeCommitsListPage(),
      },
      builder: (context, widget) {
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: FlutterEasyLoading(child: widget),
        );
      },
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      locale: const Locale('zh', 'CH'),
      supportedLocales: const [Locale('zh', 'CH')],
      theme: ThemeData.light(), // 浅色主题
      darkTheme: ThemeData.dark(), // 暗色主题
    );
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      builder: (BuildContext context, Widget? child) {
        return _materialApp(context);
      },
    );
  }
}
